﻿//<![CDATA[
Telerik.Web.UI.Editor.CommandList['MedianamikLink'] = function(commandName, editor, args) {
    var myCallbackFunction = function(sender, args) {
        var argument = getLinkElement(editor);
        var sel = editor.getSelection();
        var selectedContent = '';

        var element = editor.getSelectedElement();

        while (element && element.tagName != 'A') {
            element = element.parentNode;
        }

        if (element) {
            element.href = args.href;
            element.target = argument.target;
        }
        else {

            if (sel.getText() != '')
                selectedContent = sel.getText();
            else if (sel.getHtml() != '')
                selectedContent = sel.getHtml();
            else
                selectedContent = args.text;

            editor.pasteHtml(String.format('<a href="{0}"{1}{2}>{3}</a>', args.href, argument.target == '' ? '' : 'target="' + argument.target + '"', '', selectedContent));
            //        editor.pasteHtml(String.format('<a href="{0}"{1}{2}>{3}</a>', args.href, argument.target == '' ? '' : 'target="' + argument.target + '"', '', (sel.getText() != '' ? sel.getText() : args.text)));
            //editor.pasteHtml('<a href=".@28c68c35-d4c8-4f0d-82fe-67022ee2b942|culture=fr-CA|anchor=|protocol=auto.@">dsafasdfsdf</a>');
        }
    }

    var argument = getLinkElement(editor);

    editor.showExternalDialog(
            '/medianamik/ModalWindows/MedianamikLinkPickerWindow.aspx?href=' + argument.href,
            argument,
            340,
            500,
            myCallbackFunction,
            null,
            '',
            true,
            Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move,
            false,
            true);
};

Telerik.Web.UI.Editor.CommandList['InsertNbsp'] = function(commandName, editor, args) 
{
    editor.pasteHtml('&#x00a0;');
};

Telerik.Web.UI.Editor.CommandList['TemplateManager'] = function(commandName, editor, args)
{
    var myCallbackFunction = function(sender, args)
    {
        editor.pasteHtml(args.content);
    };
    //editor.showDialog('MedianamikTemplate', { css: cssFilePath }, myCallbackFunction);
    editor.showDialog('MedianamikTemplate', {}, myCallbackFunction);
};

Telerik.Web.UI.Editor.Modules.RadEditorDomInspector.prototype.removeSelectedElement = function(element)
{
    element.parentNode.removeChild(element);
};

function OnClientPasteHtml(sender, args)
{
    if ($telerik.isIE)
    {
        var commandName = args.get_commandName();
        var domainName = 'http://' + window.location.host;
        var filter = new Telerik.Web.UI.Editor.StripPathsFilter([domainName]);
        var contentElement = document.createElement('span');
        contentElement.innerHTML = args.get_value();
        var newElement = filter.getHtmlContent(contentElement);
        args.set_value(newElement.innerHTML);
    }
}

function getLinkElement(editor) {
    var elem = editor.getSelectedElement(); //returns the selected element.
    var argument = editor.get_document().createElement("A");

    if (elem) {
        if (elem.tagName == "A") {
            editor.selectElement(elem);
            argument = elem;
        }
        else {
            if (elem.childNodes.length < 1) {
                argument.innerHTML = elem.innerHTML;
            }
        }
    }
    return argument;
}
//]]>
